/***弹窗提示
@params title 提示文字 time 打开时间秒 icon 显示图标[none,success]
*/
function tips(title = '', time = 1500, icon = 'none') {
  wx.showToast({
    title: title,
    time: time,
    icon: icon
  })
}

/***弹窗确认提示
@params title 标题 content 内容 callback 返回函数
*/
function confirm(content='', callback) {
   wx.showModal({
     title: '温馨提示',
     content: content,
     success: function(res){
       callback(res);
     }
   })
}

/***云函数请求
@params name 云函数名称 data 参数 {action:'请求方法名',map:'查询参数',params:'参数'} callback 返回函数
*/
function coludRequest(name,data,callback){
  wx.showLoading({title: '加载中...'})
  wx.cloud.callFunction({
    name:name,
    data:data,
    success: res => {
      wx.hideLoading();
      callback(res);
    },
    fail: err => {
      console.log(err);
      wx.hideLoading();
    }
  })
}


function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()
 
  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  tips: tips,
  confirm: confirm,
  coludRequest: coludRequest,
  formatTime: formatTime
}